package Demo03;

import java.util.HashMap;

public class Test {
}

class TreeNode {
 int val = 0;
 TreeNode left = null;
 TreeNode right = null;

 public TreeNode(int val) {
     this.val = val;
 }

 }
class Solution {
    public boolean containsDuplicate(int[] nums) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(!map.containsKey(nums[i])) {
                map.put(nums[i],1);
            }else {
                return false;
            }
        }
        return true;
    }
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(!map.containsKey(nums[i])) {
                map.put(nums[i],i);
            }else {
                int index = map.get(nums[i]);
                if(i-index<=k) {
                    return true;
                }
                map.put(nums[i],i);
            }
        }
        return false;
    }
}
